// pages/operation/operation.js

let myRequest = require('../../config/myRequest.js');
let api = require('../../config/api.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    todayData: {},
    historyData: {},
    oldtabActive: 0,
    tabActive: 0,
    isPickerRender: false,
    isPickerShow: false,
    startTime: '',
    endTime: '',
    pickerConfig: {
      endDate: true, // 是否需要结束时间，为true时显示开始时间和结束时间两个picker
      column: "", //可选的最小时间范围hour、minute、secend
      dateLimit: true, //是否现在时间可选范围，false时可选任意时间；当为数字n时，范围是当前时间的最近n天
      initStartTime: " ", //picker初始时间，默认当前时间
      initEndTime: " ", //picker初始结束时间，默认当前时间
      limitStartTime: "2015-01-01", //最小可选时间
      limitEndTime: "2055-01-01" ,//最大可选时间 
      yearEnd: '2020'
    },
  },
  pickerShow: function () {
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取天  
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
    var todayTime = Y + '-' +  M  + '-' + D;
    var twoMonthTime = Y + M + D;
    
    this.setData({
      [`pickerConfig.yearEnd`]: Y ,
      [`pickerConfig.initStartTime`]: todayTime ,
      [`pickerConfig.initEndTime`]: todayTime ,
      [`pickerConfig.limitEndTime`]: todayTime ,
      isPickerShow: true,
      isPickerRender: true,
      chartHide: true
    });
  },
  pickerHide: function () {
    console.log(5465454564645)
    this.setData({
      isPickerShow: false,
      chartHide: false,
      tabActive: this.data.oldtabActive
    });
  },

  bindPickerChange: function (e) {
    console.log("picker发送选择改变，携带值为", e.detail.value);
    console.log(this.data.sensorList);
    this.getData(this.data.sensorList[e.detail.value].id);
    this.setData({
      index: e.detail.value,
      sensorId: this.data.sensorList[e.detail.value].id
    });
  },
  setPickerTime: function (val) {
    console.log(val);
    let data = val.detail;
    console.log(data);
    this.setData({
      isPickerShow: false,
      chartHide: false,
      startTime: data.startTime,
      endTime: data.endTime,
    });
    this.getData()
  },


  tabClick(e) {
    const that = this;
    console.log(e)

    this.setData({
      startTime: '',
      endTime: '',
    });

    that.setData({
      tabActive: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index != 3) {
      that.getData();
    } else {
      that.pickerShow();
    }
  },

  getData() {
    const that = this;
    that.setData({
      hiddenLoading: false
    })
    myRequest.EasyHttp.post(api.reqUrls.setreportinfo, {
      token: app.globalData.token,
      type: that.data.tabActive,
      start: that.data.startTime,
      end: that.data.endTime,

    }).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      that.setData({
        hiddenLoading: true
      })
      if (res.data.status == 1) {
        console.log(res.data, 1234)
        that.setData({
          todayData: res.data.result.todayData,
          historyData: res.data.result.historyData,
          oldtabActive: that.data.tabActive
        })

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})